package cn.itcast.nems.studentrecord.service.impl;

import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.converter.StudentRecordReferenceConverter;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecordTransaction;
import cn.itcast.nems.studentrecord.dto.change.*;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReferenceDTO;
import cn.itcast.nems.studentrecord.service.StudentRecordRepeatService;
import cn.itcast.nems.studentrecord.service.settlement.SettlementCalculatorRepeat;
import cn.itcast.nems.studentrecord.service.settlement.SettlementResult;
import cn.itcast.nems.studentrecord.service.studentrecord.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyong
 * create: 2023-12-11 19:56:22
 **/
@Service
public class StudentRecordRepeatServiceImpl extends StudentRecordChangeTargetWithOrder implements StudentRecordRepeatService {
    StudentRecordRepeatServiceImpl(StudentRecordService studentRecordService,
                                   StudentRecordTransactionService studentRecordTransactionService,
                                   StudentRecordCreateService studentRecordCreateService,
                                   StudentRecordMessageService studentRecordMessageService,
                                   StudentRecordReceivableService studentRecordReceivableService,
                                   OrderApiDelegate orderApiDelegate) {
        super(studentRecordService,
                studentRecordTransactionService,
                studentRecordCreateService,
                studentRecordMessageService,
                studentRecordReceivableService,
                orderApiDelegate);
    }

    @Override
    @Transactional
    public StudentRecordChangeResultDTO repeat(StudentRecordChangeDTO studentRecordChange) {
        StudentRecordChangeResultDTO result = super.change(studentRecordChange, ReferenceTypeEnum.REPEAT);
        if (result != null && result.isComplete()) {
            this.repeatComplete(result.getJournalDTO());
        }
        return result;
    }

    @Override
    @Transactional
    public void repeatComplete(StudentRecordJournalDTO studentRecordJournal) {
        super.changeComplete(studentRecordJournal, ReferenceTypeEnum.REPEAT);
        StudentRecord sr = super.studentRecordService.getById(studentRecordJournal.getOriginStudentRecordId());
        if (sr.getSettlementDate() == null) {
            final SettlementResult settlementResult = super.studentRecordService.studentRecordSettlement(studentRecordJournal.getOriginStudentRecordId(),
                    studentRecordJournal.getOriginClazzOutDate(),
                    new SettlementCalculatorRepeat());

            StudentRecordReferenceDTO dto = StudentRecordReferenceConverter.origin(sr, studentRecordJournal);
            logger.info("重读完成，创建原班学籍流转信息，学籍ID: '{}' {}", sr.getId(), dto);
            super.createStudentRecordTransaction(dto, settlementResult, StudentRecordTransaction.TYPE_OUT);
            super.studentRecordService.unlock(sr.getId());
        } else {
            logger.warn("学籍{}已经结算过", sr.getId());
        }
    }

    @Override
    StudentRecordChangeChecker createStudentRecordChangeChecker(StudentRecordChangeCheckOriginDTO studentRecordOrigin,
                                                                List<StudentRecordTransactionChangeCheckDTO> transactions) {
        return new StudentRecordChangeRepeatChecker(studentRecordOrigin, transactions);
    }
}
